package com.tronsis.library.util;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.widget.Toast;


import com.tronsis.library.R;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * @author scofield@tronsis.com
 * @date 2018/8/1 11:45
 */
public class NotificationUtil {

    public static final String CHANNEL_ID_DEVICE = "device_event";
    public static final String CHANNEL_ID_APP = "app_message";

    /**
     * targetSdkVersion >=26 通知必须设置Channel
     *
     * @param context
     */
    public static void createNotificationChannel(Context context) {
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = context.getString(R.string.device_event_reminder);
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(context, CHANNEL_ID_DEVICE, channelName, importance);

            channelName = context.getString(R.string.app_message_reminder);
            importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(context, CHANNEL_ID_APP, channelName, importance);
        }*/
    }


    @TargetApi(Build.VERSION_CODES.O)
    public static void createNotificationChannel(Context context, String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
//        channel.enableLights(true); //是否在桌面icon右上角展示小红点
//        channel.setLightColor(Color.RED); //小红点颜色
//        channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

    /**
     * 通知channel 是否被关闭
     * @param context
     * @param channelId
     * @return
     */
    public  static boolean isChannelClosed(Context context, String channelId) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel(channelId);
            return channel.getImportance() == NotificationManager.IMPORTANCE_NONE;
        }

        return false;
    }

    /**
     * 跳转到设置，提醒打开已关闭的通知 channel
     * @param context
     * @param channelId
     */
    @TargetApi(Build.VERSION_CODES.O)
    public static void openChannelNotifacition(Context context, String channelId) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel(channelId);
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                context.startActivity(intent);
                Toast.makeText(context, "请手动将通知打开", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public static void showNotification(Context context, Class<?> cls, String title, String body, String channelId) {
        /*Intent intent = new Intent(context, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,channelId);
        builder.setTicker(TextUtils.isEmpty(title) ? context.getString(R.string.have_new_notification) : title);//状态栏显示内容
        builder.setContentText(TextUtils.isEmpty(body) ? title : body);
        builder.setContentTitle(TextUtils.isEmpty(title) ? context.getString(R.string.click_to_see_more) : title);
        builder.setDefaults(Notification.DEFAULT_ALL);//接收到通知时，按手机的默认设置进行处理，声音，震动，灯
        //通知栏显示图标
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            builder.setSmallIcon(R.mipmap.ic_launcher);//7.0需要使用透明的icon 否则显示为白色
        } else {
            builder.setSmallIcon(R.mipmap.ic_launcher);
        }
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));//todo 应用图标要为drawable
        builder.setColor(context.getResources().getColor(R.color.colorPrimary));
        builder.setContentIntent(pendingIntent);

        Notification notification = builder.build();
        //点击跳转后消失
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        int notifyId = (int) System.currentTimeMillis();
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.notify(notifyId, notification);
        }*/
    }
}
